﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinForm.Contorls.Docking;

namespace IFTRUE.MongoWin
{
    public partial class MongoManageMain : Form
    {
        ServerListManage m_toolbox = new ServerListManage();
        
        private int childFormNumber = 0;

        public MongoManageMain()
        {
            InitializeComponent();
        }
               

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            //foreach (Form froms in this.MdiChildren)
            //{
            //    if (froms is CreateServer)
            //    {
            //        froms.Focus();
            //        return;
            //    }
            //}

            CreateServer frmCreateServer = new CreateServer();
            //frmCreateServer.MdiParent = this;
            //dockPanel.Controls.Add(frmCreateServer);
            //frmCreateServer.Show();
            frmCreateServer.ShowDialog();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            ConnectionServer frmConnectionServer = new ConnectionServer();
            frmConnectionServer.ShowDialog();
        }

        private void MongoManageMain_Load(object sender, EventArgs e)
        {
            ShowToolVeiw();
        }

        /// <summary>
        /// 关闭Dock
        /// </summary>
        private void CloseAllContents()
        {            
            m_toolbox.DockPanel = null;
         
            // Close all other document windows
            CloseAllDocuments();
        }

        /// <summary>
        /// 释放对象
        /// </summary>
        private void CloseAllDocuments()
        {
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                foreach (Form form in MdiChildren)
                    form.Close();
            }
            else
            {
                IDockContent[] documents = dockPanel.DocumentsToArray();
                foreach (IDockContent content in documents)
                    content.DockHandler.Close();
            }
        }

        private void btnShowSml_Click(object sender, EventArgs e)
        {
            ShowToolVeiw();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            SQLPlusDoc dummyDoc = CreateNewDocument();
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                dummyDoc.MdiParent = this;                 
                dummyDoc.Show();
            }
            else
                dummyDoc.Show(dockPanel);            
        }

        private SQLPlusDoc CreateNewDocument()
        {
            SQLPlusDoc dummyDoc = new SQLPlusDoc();

            int count = 1;
            string text = "MongoQuery" + count.ToString();
            while (FindDocument(text) != null)
            {
                count++;               
                text = "MongoQuery" + count.ToString();
            }
            dummyDoc.Text = text;
            return dummyDoc;
        }

        private IDockContent FindDocument(string text)
        {
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                foreach (Form form in MdiChildren)
                    if (form.Text == text)
                        return form as IDockContent;

                return null;
            }
            else
            {
                foreach (IDockContent content in dockPanel.Documents)
                    if (content.DockHandler.TabText == text)
                        return content;

                return null;
            }
        }


        private void btnSave_Click(object sender, EventArgs e)
        {

        }

        private void btnShowHelp_Click(object sender, EventArgs e)
        {
            string exPath = System.Environment.CurrentDirectory;
            //MessageBox.Show(dd);
            Help.ShowHelp(null, exPath + "\\" + "MongoDBHelp.chm", HelpNavigator.Find, "Golbal");
        }

        protected void ShowToolVeiw()
        {
            m_toolbox.Show(dockPanel);
        }
    }
}
